package com.ruoyi.GoApply.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.RegisterBody;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.outuser.domain.OutUser;
import com.ruoyi.place.domain.Place;
import com.ruoyi.place.mapper.PlaceMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.web.controller.system.SysRegisterController;
import com.ruoyi.web.controller.system.SysUserController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import com.ruoyi.GoApply.mapper.GoApplyMapper;
import com.ruoyi.GoApply.domain.GoApply;
import com.ruoyi.GoApply.service.IGoApplyService;

import javax.annotation.Resource;

import static com.ruoyi.common.core.domain.AjaxResult.error;
import static com.ruoyi.common.core.domain.AjaxResult.success;

/**
 * 入驻申请Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-03
 */
@Service
public class GoApplyServiceImpl implements IGoApplyService 
{
    @Autowired
    private GoApplyMapper goApplyMapper;

    /**
     * 查询入驻申请
     * 
     * @param id 入驻申请主键
     * @return 入驻申请
     */
    @Override
    public GoApply selectGoApplyById(Long id)
    {
        return goApplyMapper.selectGoApplyById(id);
    }

    /**
     * 查询入驻申请列表
     * 
     * @param goApply 入驻申请
     * @return 入驻申请
     */
    @Override
    public List<GoApply> selectGoApplyList(GoApply goApply)
    {
        return goApplyMapper.selectGoApplyList(goApply);
    }

    /**
     * 新增入驻申请
     * 
     * @param goApply 入驻申请
     * @return 结果
     */
    @Override
    public int insertGoApply(GoApply goApply)
    {
        return goApplyMapper.insertGoApply(goApply);
    }

    /**
     * 修改入驻申请
     * 
     * @param goApply 入驻申请
     * @return 结果
     */
    @Autowired
    SysRegisterController sysRegisterController;
    @Autowired
    GoApplyMapper applyMapper;
    @Autowired
    PlaceMapper placeMapper;
    @Override
    public AjaxResult updateGoApply(GoApply apply)
    {
        applyMapper.deleteGoApplyById(apply.getId());
        String msg="";
        RegisterBody r=new RegisterBody();
        if(apply.getStatus()){
            r.setUsername(apply.getPhone());
            String password = IdUtils.fastSimpleUUID();
            r.setPassword(password);
            r.setOutusername(apply.getUsername());


             msg = sysRegisterController.register(r);
             if(StringUtils.isEmpty(msg)){
                 Place p=new Place();
                 p.setName(apply.getName());
                 p.setInfo(apply.getInfo());
                 p.setImage(apply.getImg());
                 p.setPosition(apply.getPosition());
                 placeMapper.insertPlace(p);
             }
            return StringUtils.isEmpty(msg) ? success("success",r) : success("error","审核失败，"+msg);
        }else{
            return success("success");
        }
    }

    /**
     * 批量删除入驻申请
     * 
     * @param ids 需要删除的入驻申请主键
     * @return 结果
     */
    @Override
    public int deleteGoApplyByIds(Long[] ids)
    {
        return goApplyMapper.deleteGoApplyByIds(ids);
    }

    /**
     * 删除入驻申请信息
     * 
     * @param id 入驻申请主键
     * @return 结果
     */
    @Override
    public int deleteGoApplyById(Long id)
    {
        return goApplyMapper.deleteGoApplyById(id);
    }
}
